/*jslint vars:true plusplus:true*/

//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author:
// File: 
// Description:
//=========================================================
function Bullet(spriteSrc, jumpVelocity) {
    "use strict";
    var self = this;
    var startHeight = 200;
    var sprite = new Sprite(spriteSrc, function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = -sprite.image.height;
    });
    
    var entity = new Entity(sprite, 100, startHeight);
    
    // the buggy has been hit
    this.onHit = function () {
        // on hit functionality here
        self.setEnabled(false);
    };

     this.setEnabled = function(enabled){
        entity.isVisible = enabled;
        collider.isEnabled = enabled;
    };
    
    
    var collider = new Collider(entity, 10,window.game.enemyLayer, 0, 0, self.onHit);
    
    var y_velocity = 50;
    
    var y_accelleration = 50;

    
    var x_velocity = 70;

    // update the Bullet every frame
    this.update = function () {
       
        if (collider.isEnabled){
            y_velocity += y_accelleration/window.game.frameRate;

       // update y
            entity.y += y_velocity / window.game.frameRate;

            entity.x += x_velocity / window.game.frameRate;

            if (entity.y > window.game.background.getGroundLevel())
            {
                window.game.hazards.spawnPit(entity.x);
                self.setEnabled(false);
                
                var audio = new Audio('audio/bang1.ogg');
                audio.play();
            }
        }
        
    };
    
  //
    this.start = function ( x,y,x_vel, y_vel) {
      y_velocity = y_vel;
      x_velocity = x_vel;
        entity.x = x;
        entity.y = y;
        self.setEnabled(true);
    };
    
    self.setEnabled(false);
}

function UfoExplode(spriteSrc, jumpVelocity) {
    "use strict";
    var self = this;
    var startHeight = 200;
     var sprite = new Sprite(spriteSrc, function (sprite) {
        sprite.offsetX = -75;
        sprite.offsetY = -25;
    }, 151, 63);
    
    var entity = new Entity(sprite, 100, startHeight);
    
    entity.isVisible = false;
    
    var explosioncount = 20;
    var explosiontimer = 5;
    // update the explosion every frame
    this.update = function () {
        explosioncount--;
        if (explosioncount === 0){
            entity.incSprite();
            explosioncount = 20;
        }
        
        explosiontimer -= 1/window.game.frameRate;
        if (explosiontimer < 0)
        {
            entity.isVisible = false;
        }
    };
    
    
  //
    this.start = function ( x,y,x_vel, y_vel) {
        
        entity.isVisible = true;
        
        explosiontimer = 5;
              var audio = new Audio('audio/ufoexplode.ogg');
            audio.play();
        
        
        
      //y_velocity = y_vel;
    //  x_velocity = x_vel;
        entity.x = x;
        entity.y = y;
    };
}


function Ufo(spriteSrc, startX, startY) {
    "use strict";
    var self = this;
    var sprite = new Sprite(spriteSrc, function (sprite) {
        sprite.offsetX = -75;
        sprite.offsetY = -25;
    }, 151, 63);
    
    var entity = new Entity(sprite, startX, startY);
    
    // the buggy has been hit
    this.onHit = function () {
        // on hit functionality here
        explosion.start(entity.x, entity.y+25,x_velocity,y_velocity);
        entity.isVisible = false;
        collider.isEnabled = false;
        respawn_timer = 10;
        isdead = true;
        window.game.scoresystem.HitUfo();
    };

    
    this.setactive = function(active) {
         entity.isVisible = active && !isdead;
         collider.isEnabled = active && !isdead;
        self.isactive = active;
    }
    
    var collider = new Collider(entity, 50, window.game.enemyLayer, 0,0, self.onHit);
    
    var isdead = false;
    
    self.isactive = true;
    
    var y_velocity = 50;
    
    var y_timer = 3;
    
    var x_timer = 5;
    
    var x_velocity = 70;
    
    var bullet_timer = 4;
    var respawn_timer = 4;
    var bullet = new Bullet("images/Bullet 3.png", -500);
    var explosion = new UfoExplode("images/explosion Spliced.png");
    
    var lightcount = 20;
    
    
    
    window.game.addSubsystem(bullet);
    window.game.addSubsystem(explosion);   
    
    // update the ufo every frame
    this.update = function () {
        if(self.isactive) {
            if (!isdead ){




                y_timer -= 1/window.game.frameRate;
                x_timer -= 1/window.game.frameRate;
                bullet_timer -= 1/window.game.frameRate;

                lightcount = lightcount -1;


                if (lightcount === 0){
                    entity.incSprite();
                    lightcount = 20;
                }



                if (y_timer < 0 || entity.y < 40 || entity.y > 400) {

                    y_timer = 3 + Math.random() * 3;

                    y_velocity = -y_velocity; 

                }


                if (x_timer < 0 || entity.x < 20 || entity.x > 600) {

                    x_timer = 5 + Math.random() * 3;

                    x_velocity = -x_velocity;



                }

                if (bullet_timer < 0) {

                    bullet_timer = 3 + Math.random() * 3;
                    bullet.start (entity.x, entity.y+25,x_velocity,50);
                var audio = new Audio('audio/laser1.ogg');
                audio.play();
                }


           // update y
                entity.y += y_velocity / window.game.frameRate;
                entity.x += x_velocity / window.game.frameRate;
            }
            else // we are dead
            {
                respawn_timer -= 1/window.game.frameRate;

                if (respawn_timer < 0) {
                    entity.isVisible = true;
                    collider.isEnabled = true;
                    isdead = false;

                    entity.y = 100;
                    entity.x = 150;
                }
            }
        }
    };
    
  //
    
    
}

function UfoSystem () {
    
    var ufoCount = 20;
    var ufos = [];
    
    
    
    for (var ufoidx = 0; ufoidx < ufoCount; ufoidx++) {
        var ufo = new Ufo("images/UfoSpliced2.png", Math.random() *400, Math.random() * 300);
        ufo.setactive(false);
        ufos.push(ufo);
    }
    
    
    this.activate = function(num){
        
        if (num > ufoCount)
            num = ufoCount;
        
        for (var ufoidx = 0; ufoidx < ufoCount; ufoidx++)
        {
            ufos[ufoidx].setactive(ufoidx < num);
        }
    }
    
    this.update = function () {
        for (var ufoidx = 0; ufoidx < ufos.length; ufoidx++) {
            ufos[ufoidx].update();
        }
    }
    
    
}




function CheckpointSystem () {
    
    var checkpoints = [{hazards:true, ufocount:0, time:30, hazardMin:3, hazardMax:12}, 
                      {hazards:false, ufocount:1, time:60, hazardMin:2.5, hazardMax:10}, 
                      {hazards:false, ufocount:2, time:90, hazardMin:2, hazardMax:8}, 
                      {hazards:true, ufocount:2, time:120, hazardMin:1.5, hazardMax:6}, 
                      {hazards:true, ufocount:4, time:180, hazardMin:1, hazardMax:4},
                        {hazards:true, ufocount:5, time:1800000000000, hazardMin:0.5, hazardMax:2}
                      ];
    
    
    var timer = 0;
    var checkpointIdx = 0;
    
    this.update = function () {
        timer += 1/ window.game.frameRate;
            
        
        if (timer > checkpoints[checkpointIdx].time){
            checkpointIdx++;
            window.game.ufos.activate(checkpoints[checkpointIdx].ufocount);
            window.game.hazards.enabled = checkpoints[checkpointIdx].hazards;
            window.game.hazards.minSpawn = checkpoints[checkpointIdx].hazardMin;
            window.game.hazards.minSpawn = checkpoints[checkpointIdx].hazardMax;
        }
    }
    
    this.reset = function() {
        timer = 0;
        window.game.ufos.activate(checkpoints[0].ufocount);
        window.game.hazards.enabled = checkpoints[0].hazards;
        window.game.hazards.minSpawn = checkpoints[0].hazardMin;
        window.game.hazards.minSpawn = checkpoints[0].hazardMax;
        
    }
    
    window.game.ufos.activate(checkpoints[0].ufocount);
    window.game.hazards.enabled = checkpoints[0].hazards;
    window.game.hazards.minSpawn = checkpoints[0].hazardMin;
    window.game.hazards.minSpawn = checkpoints[0].hazardMax;
        
}

// add the buggy as a subsystem

window.game.ufos = new UfoSystem();
window.game.checkpoints = new CheckpointSystem();
window.game.addSubsystem(window.game.ufos);
window.game.addSubsystem(window.game.checkpoints);
